<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_ActionDao extends Vega_Dao_Table
{
    public function getDetail($actionId)
    {
        $actionId = $this->_db->valueQuote($actionId);
        $sql = "SELECT * FROM admin_action WHERE id=$actionId";
        $this->_db->query($sql);
        
        return $this->_db->fetchRow();
    }

    public function getActionListFromModule($moduleName)
	{
        $moduleName = $this->_db->valueQuote($moduleName);
        $sql = "SELECT * FROM admin_action WHERE module=$moduleName";
        $this->_db->query($sql);
        
        return $this->_db->fetchRowset();
	}

    public function getList()
    {
        $sql = "SELECT id FROM admin_action";
        $this->_db->query($sql);

        return $this->_db->fetchRowset();
    }
    
    public function getModuleList()
    {
        $sql = "SELECT DISTINCT(module) AS name FROM admin_action";
        $this->_db->query($sql);
        
        return $this->_db->fetchRowset();
    }

    public function insert($data)
    {
        $name = $this->_db->valueQuote($data['name']);
        $description = $this->_db->valueQuote($data['description']);
        $module = $this->_db->valueQuote($data['module']);

        $sql = "INSERT INTO admin_action SET name=$name, description=$description, module=$module, updated_by=".$_SESSION['userid'].", updated_time=now()";

        return $this->_db->queryWrite($sql);
    }
    
    public function save($actionId, $data)
    {
        $actionId = intval($actionId);
        $name = $this->_db->valueQuote($data['name']);
        $description = $this->_db->valueQuote($data['description']);
        $module = $this->_db->valueQuote($data['module']);

        $sql = "UPDATE admin_action SET name=$name, description=$description, module=$module, updated_by=".$_SESSION['userid'].", updated_time=now() WHERE id=$actionId";

        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list)
    {
        $list = "(" . implode(",", $list) . ")";
        $sql = "DELETE FROM admin_action WHERE id IN $list";

        return $this->_db->queryWrite($sql);
    }
}
?>